<?php
include 'PayFunction.php';
/*
 * 省级联动
 */
function getCity($parentId){
    if(isset($parentId)){
        $city_list=M("region")->where("parent_id='$parentId'")->select();
        return $city_list;
    }else{
        return null;
    }
}
function getXian($parentId){
    if(isset($parentId)){
        $city_list=M("region")->where("parent_id='$parentId'")->select();
        return $city_list;
    }else{
        return null;
    }
}

/*
 * 商品类别联查
 */
function getcategory($parentId){
    if(isset($parentId)){
        $goods_list=M("category")->where("parent_id='$parentId'")->select();
        return $goods_list;
    }else{
        return null;
    }
}
// 根据地址ID检索详情
function getRegionName($id){
    if(isset($id)) {
        $request = M('Region')->field('name_path')->where("id='{$id}'")->find();
        return !empty($request)?$request['name_path']:'无地址';
    }else{
        return '';
    }
}

// 根据分类，获取分类名称
function getCategoryName($id){
    if(isset($id)) {
        $request = M('Category')->field('name_path')->where("id='{$id}'")->find();
        return !empty($request)?$request['name_path']:'无分类';
    }else{
        return '';
    }
}

/**
 *  给分页传参数
 * @param  mixed $Page 分页对象
 * @param array $parameter 传参数组
 */
function setPageParameter($Page,$parameter){
    foreach ($parameter as $k=> $v){
        if (isset($v)){
            $Page->parameter[$k]=$v;
        }
    }
}



function inputToParam($param_field)
{
    $param = array();

    foreach ($param_field as  $value) {
        if (I($value) != null || I($value) != '') {
            $param[$value] = I($value);
        }
    }
    return $param;
}
/**
 * 判断是否为手机登录
 * @return bool
 */

function ismobile() {
    // 只要手机版
    return true;
     // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    /*if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        return true;

    //此条摘自TPM智能切换模板引擎，适合TPM开发
    if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
        return true;
    //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
        //找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
    //判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
        //从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    //协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;*/
}


/**
 * 推送需要的参数
 * $receive = 'all'; //全部
 * $receive = array('tag'=>array('2401','2588','9527')); //标签
 * $receive = array('alias'=>array('93d78b73611d886a74*****88497f501')); //别名
 * $content = '推送数据';
 * $m_type = 'http';
 * $m_txt = 'http://www.baidu.com/';
 * $m_time = '600'; //离线保留时间
 **/
/**
 * 向用户推送消息
 * @param $tag 用户tag
 * @param $content 内容
 */
function send_pub($tag,$content)
{
    $push = new Home\Controller\JPushController();
    //推送附加字段的类型
    $m_type = 'http';
    //推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。
    $m_txt = 'http://www.lehmall.com/';
    //离线保留时间
    $m_time = '86400';
    //推送接收人
    //$receive = "all";
    $receive = array('tag'=>array($tag));
    //存储推送状态
    $message = "";
    $result = $push->push($receive, $content, $m_type, $m_txt, $m_time);
    if ($result) {
        $res_arr = json_decode($result, true);
        //如果返回了error则证明失败
        if (isset($res_arr['error'])) {
            //错误信息
            //$msg = $res_arr['error']['message'];
            //返回码码
            $error_code = $res_arr['error']['code'];
            switch ($error_code) {
                case 200:
                    $message = '发送成功！';
                    break;
                case 1000:
                    $message = '失败(系统内部错误)';
                    break;
                case 1001:
                    $message = '失败(只支持 HTTP Post 方法，不支持 Get 方法)';
                    break;
                case 1002:
                    $message = '失败(缺少了必须的参数)';
                    break;
                case 1003:
                    $message = '失败(参数值不合法)';
                    break;
                case 1004:
                    $message = '失败(验证失败)';
                    break;
                case 1005:
                    $message = '失败(消息体太大)';
                    break;
                case 1008:
                    $message = '失败(appkey参数非法)';
                    break;
                case 1020:
                    $message = '失败(只支持 HTTPS 请求)';
                    break;
                case 1030:
                    $message = '失败(内部服务超时)';
                    break;
                default:
                    $message = '失败(返回其他状态，目前不清楚额，请联系开发人员！)';
                    break;
            }
        } else {
            $message = "发送成功！";
        }
    } else {
        $message = '接口调用失败或无响应';
    }
    return $message;
}

//科学计数法数字，还原
function NumEToNum($num){
    if (stripos($num,'E')===false) return $num;
    $nums = explode('E',$num);
    $num1 = $nums[0];
    $num2 = $nums[1];
    $result = $num1*pow(10,$num2);
    return $result;
}

